package com.ITXu.reggie.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ITXu.reggie.service.AddressBookService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ITXu.reggie.entity.AddressBook;
import com.ITXu.reggie.commom.BaseContext;
import com.ITXu.reggie.commom.R;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/addressBook")
@CrossOrigin
@Slf4j
public class AddressBookController {

	@Autowired
	private AddressBookService addressBookService;
	
	/**
	 * 新增
	 * @param addressBook
	 * @return
	 */
	@PostMapping
	public R<AddressBook> save(@RequestBody AddressBook addressBook) {
		log.info(addressBook.toString());
		addressBook.setUserId(BaseContext.getCurrentId());
		addressBookService.save(addressBook);
		return R.success(addressBook);
	}
	
	/**
	 * 设置默认地址
	 * @param addressBook
	 * @return
	 */
	@PutMapping("default")
	public R<AddressBook> setDefaultAddress(@RequestBody AddressBook addressBook) {
		//把所有当前userId的addressbook的isdefault设置为0
		LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
		wrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
		wrapper.set(AddressBook::getIsDefault, 0);
		addressBookService.update(wrapper);
		//设置当前addressbook的isdefault为1
		addressBook.setIsDefault(1);
		addressBookService.updateById(addressBook);
		return R.success(addressBook);
	}
	
	/**
	 * 根据id查用户(这里应该是一个list集合)
	 * @param id
	 * @return
	 */
	@GetMapping("/{id}")
	public R<AddressBook> getById(@PathVariable Long id) {
		AddressBook addressBook = addressBookService.getById(id);
		if (addressBook != null) {
			return R.success(addressBook);
		}
		return R.error("该用户不存在");
	}
	
	/**
	 * 查找默认addressbook
	 * @return
	 */
	@GetMapping("default")
	public R<AddressBook> getDefaultAddress() {
		//根据当前userId查询isdefault为1的addressbook
		LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
		queryWrapper.eq(AddressBook::getIsDefault, 1);
		AddressBook currentAddressBook = addressBookService.getOne(queryWrapper);
		if (currentAddressBook == null) {
			return R.error("没有找到该对象");
		}
		return R.success(currentAddressBook);
	}
	
	/**
	 * 查询指定用户的全部地址
	 * @param addressBook
	 * @return
	 */
	@GetMapping("/list")
	public R<List<AddressBook>> list(AddressBook addressBook) {
		//addressBook.setUserId(BaseContext.getCurrentId());
		LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
		queryWrapper.orderByDesc(AddressBook::getUpdateTime);
		List<AddressBook> addressBooks = addressBookService.list(queryWrapper);
		return R.success(addressBooks);
	}
}
